package cn.com.haopy.yue.ai.db.service.hos.impl;

import cn.com.haopy.yue.ai.db.entity.arisk.AIRiskCheckIndex;
import cn.com.haopy.yue.ai.db.entity.arisk.AIRiskUserHumanQueue;
import cn.com.haopy.yue.ai.db.mapper.arisk.AIRiskUserHumanQueueMapper;
import cn.com.haopy.yue.ai.db.service.BaseServiceImpl;
import cn.com.haopy.yue.ai.db.service.hos.AIRiskUserHumanQueueService;
import cn.com.haopy.yue.ai.db.utils.SqlInjectionExtUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @Description: TODO
 * @Author DuTao
 * @Date 2025/4/14 17:17
 */
@Service
public class AIRiskUserHumanQueueServiceImpl extends BaseServiceImpl<AIRiskUserHumanQueueMapper, AIRiskUserHumanQueue>
        implements AIRiskUserHumanQueueService {

    @Override
    public AIRiskUserHumanQueue getQueueByUserId(Integer userId) {
        LambdaQueryWrapper<AIRiskUserHumanQueue> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AIRiskUserHumanQueue::getUserId, userId);
        queryWrapper.eq(AIRiskUserHumanQueue::getIsAllot, Boolean.FALSE);
        return super.getOne(queryWrapper);
    }

    @Override
    public Integer queueCount() {
        return super.baseMapper.queueCount() ;
    }

    /**
     * 我的排队位置
     *
     * @param queueId
     * @return
     */
    @Override
    public Integer myQueueIndex(Integer queueId) {
        return super.baseMapper.myQueueIndex(queueId);
    }

    @Override
    public Page<AIRiskUserHumanQueue> querySelective(Integer page, Integer limit, String sort, String order) {
        if (SqlInjectionExtUtils.check(sort) || SqlInjectionExtUtils.check(order)) {
            throw new RuntimeException("存在非法字段");
        }
        QueryWrapper<AIRiskUserHumanQueue> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AIRiskUserHumanQueue::getIsAllot, 0);
        if (StringUtils.hasText(sort) && StringUtils.hasText(order)) {
            queryWrapper.orderBy(true, "asc".equals(order), sort);
        }
        return super.page(new Page<>(page, limit), queryWrapper);
    }
}
